//
//  NetworkStatusViewController.m
//  WebPDA
//
//  Created by Justin Yip on 8/7/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "NetworkStatusViewController.h"
#import "Reachability.h"


@implementation NetworkStatusViewController

@synthesize wifiImageView;
@synthesize cellularImageView;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
	cellularImageView.image = [UIImage imageNamed:@"stop-32.png"];
	wifiImageView.image = [UIImage imageNamed:@"stop-32.png"];
	
	NetworkStatus netStatus = [curReach currentReachabilityStatus];
	switch (netStatus) {
		case NotReachable:
			NSLog(@"no reachable");
			break;
		case ReachableViaWWAN:
			NSLog(@"wwan");
			cellularImageView.image = [UIImage imageNamed:@"WWAN5.png"];
			break;
		case ReachableViaWiFi:
			NSLog(@"wifi");
			wifiImageView.image = [UIImage imageNamed:@"WWAN5.png"];
			break;
		default:
			abort();
	}
}

//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
	NSLog(@"reachability chaged");
	Reachability* curReach = [note object];
	NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
	[self updateInterfaceWithReachability: curReach];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad {
	hostReach = [[Reachability reachabilityWithHostName: @"www.baidu.com"] retain];
	[hostReach startNotifier];
	[self updateInterfaceWithReachability: hostReach];
	
    internetReach = [[Reachability reachabilityForInternetConnection] retain];
	[internetReach startNotifier];
	[self updateInterfaceWithReachability: internetReach];
	
    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
	[wifiReach startNotifier];
	[self updateInterfaceWithReachability: wifiReach];
	
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)dealloc {
	[wifiImageView release];
	[cellularImageView release];
    [super dealloc];
}


@end
